View Javadoc
1   /*
2    * DatabaseBuilder.java
3    *
4    * Created on December 23, 2004, 11:09 AM
5    */
6   
7   package gov.noaa.eds.xapi.mock;
8   
9   import gov.noaa.eds.xapi.generic.GenericCollection;
10  import gov.noaa.eds.xapi.generic.GenericDatabase;
11  import gov.noaa.eds.xapi.generic.GenericDomHandlerResource;
12  import gov.noaa.eds.xapi.generic.handlers.ResourceHandler;
13  import java.io.IOException;
14  import java.util.ArrayList;
15  import java.util.Properties;
16  import org.apache.log4j.Logger;
17  import org.xmldb.api.base.Database;
18  import org.xmldb.api.base.XMLDBException;
19  
20  /***
21   *
22   * @author tns
23   */
24  public class DatabaseBuilder {
25      
26      private static Logger log = Logger.getLogger(DatabaseBuilder.class);
27      
28      private String[] resources = null;
29      private String collectionName = null;
30      
31      public DatabaseBuilder(){
32          this("xmldb:generic://eatmutton.com/Mock Collection");
33      }
34      
35      
36      /*** Creates a new instance of DatabaseBuilder */
37      public DatabaseBuilder(String collectionName) {
38          Properties prop = new Properties();
39          try {
40              prop.load( this.getClass().getClassLoader().getResourceAsStream("mock/resources.properties"));
41              this.init(collectionName,prop);
42          } catch (IOException e){
43              throw new RuntimeException(e);
44          }
45          
46      }
47      
48      public DatabaseBuilder(String collectionName, Properties properties){
49          String resourceString = properties.getProperty("xapi.mock.resources");
50          String[] xResources = resourceString.split(":");
51          this.init(collectionName,xResources);
52      }
53      
54      public DatabaseBuilder(String collectionName, String[] resources){
55          this.init(collectionName,resources);
56      }
57      
58      private void init(String collectionName, Properties properties){
59          String resourceString = properties.getProperty("xapi.mock.resources");
60          String[] resources = resourceString.split(":");
61          this.init(collectionName,resources);
62      }
63      
64      private void init(String collectionName, String[] resources){
65          this.collectionName = collectionName;
66          this.resources = resources;
67      }
68      
69      public Database build() throws XMLDBException {
70          GenericCollection collection = new GenericCollection();
71          collection.setName(this.collectionName);
72          //lets load the resources first
73          ArrayList xmlResources = new ArrayList();
74          for (int i = 0; i < resources.length; i++){
75              ResourceHandler handler = new ResourceHandler();
76              handler.setResource(resources[i]);
77              GenericDomHandlerResource resource = new GenericDomHandlerResource();
78              resource.setId(Integer.toString(i));
79              resource.setDomHandler(handler);
80              collection.storeResource(resource);
81          }
82          GenericDatabase database = new GenericDatabase();
83          database.addCollection(collection);
84          return database;
85      }
86      
87      
88  }